<?php

class Encryption 
{
	
	var $__crypher = 'blowfish';
	var $__mode = 'cfb';
	var $__key = '';
	
	public function __construct() 
	{
		$this->__crypher = Config::get('encryptor_crypher');
		$this->__mode = Config::get('encryptor_mode');
		$this->__key = Config::get('encryptor_key');
	}
	
	public function setCrypher($crypher) 
	{
		$this->__crypher = $crypher;
	}
	
	public function getCrypher() 
	{
		return $this->__crypher;
	}
	
	public function setMode($mode) 
	{
		$this->__mode = $mode;
	}
	
	public function getMode() 
	{
		return $this->__mode;
	}
	
	public function setKey($key) 
	{
		$this->__key = $key;
	}
	
	public function getKey() 
	{
		return $this->__key;
	}
	
	public function encrypt($string) 
	{
		$td = mcrypt_module_open($this->getCrypher(), '', $this->getMode(), '');
		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		mcrypt_generic_init($td, $this->getKey(), $iv);
		$text = mcrypt_generic($td, $string);
		mcrypt_generic_deinit($td);
		
		return $iv . $text;
	}
	
	public function decrypt($string) 
	{
		$td = mcrypt_module_open($this->getCrypher(), '', $this->getMode(), '');
		$ivSize = mcrypt_enc_get_iv_size($td);
		$iv = substr($string, 0, $ivSize);
		$string = substr($string, $ivSize);
		$decrypted = '';
		if ($iv) {
			mcrypt_generic_init($td, $this->getKey(), $iv);
			$decrypted = mdecrypt_generic($td, $string);
			mcrypt_generic_deinit($td);
		}
		return $decrypted;
	}
	
}

?>